<?php

namespace eagle\modules\permission\apihelpers;

use eagle\modules\permission\helpers\UserHelper;
class UserApiHelper {
	
	/**
	 +--------------------------------------------------------------------------------------------------------
	 * 检查用户模块权限
	 +--------------------------------------------------------------------------------------------------------
	 * @access static
	 +--------------------------------------------------------------------------------------------------------
	 * @param $module	模块名称 , $module=\Yii::$app->controller->module->id;获取到的模块id 或者是UserHelper::$modulesKeyNameMap 里面对应模块的key值
	 * @param $uid	用户id
	 +--------------------------------------------------------------------------------------------------------
	 * @return boolean
	 +--------------------------------------------------------------------------------------------------------
	 **/
	public static function checkModulePermission($module,$uid=false){
		return UserHelper::checkModulePermission($module,$uid);
	}
	
	/**
	 +--------------------------------------------------------------------------------------------------------
	 * 检查用户平台权限
	 +--------------------------------------------------------------------------------------------------------
	 * @access static
	 +--------------------------------------------------------------------------------------------------------
	 * @param $platform	平台名称,PlatformAccountApi::$platformList 里面的平台
	 * @param $uid	用户id
	 +--------------------------------------------------------------------------------------------------------
	 * @return boolean
	 +--------------------------------------------------------------------------------------------------------
	 **/
	public static function checkPlatformPermission($platform,$uid=false){
		return UserHelper::checkPlatformPermission($platform,$uid);
	}
	
	/**
	 +--------------------------------------------------------------------------------------------------------
	 * 检查用户其他模块权限
	 +--------------------------------------------------------------------------------------------------------
	 * @access static
	 +--------------------------------------------------------------------------------------------------------
	 * @param $other	其他功能，如例如统计'profit'。具体输入值参考UserHelper::$ohtersKeyNameMap 里面的key
	 * @param $uid	用户id
	 +--------------------------------------------------------------------------------------------------------
	 * @return boolean
	 +--------------------------------------------------------------------------------------------------------
	 **/
	public static function checkOtherPermission($other,$uid=false){
		return UserHelper::checkOtherPermission($other,$uid);
	}
	
	/**
	 +--------------------------------------------------------------------------------------------------------
	 * 检查用户设置权限，lrq20170829
	 +--------------------------------------------------------------------------------------------------------
	 * @access static
	 +--------------------------------------------------------------------------------------------------------
	 * @param $module	设置名称 , UserHelper::$SettingmodulesKeyNameMap 里面对应模块的key值
	 * @param $uid	用户id
	 +--------------------------------------------------------------------------------------------------------
	 * @return boolean
	 +--------------------------------------------------------------------------------------------------------
	 **/
	public static function checkSettingModulePermission($module,$uid=false){
		return UserHelper::checkSettingModulePermission($module,$uid);
	}
	
	/**
	 +--------------------------------------------------------------------------------------------------------
	 * 检查用户子操作权限，lrq20180423
	 +--------------------------------------------------------------------------------------------------------
	 * @return boolean
	 +--------------------------------------------------------------------------------------------------------
	 **/
	public static function checkModuleChliPermission($module_clid,$uid=false){
		return UserHelper::checkModuleChliPermission($module_clid,$uid);
	}
	
	/**
	 +--------------------------------------------------------------------------------------------------------
	 * 查询设置权限版本号，lrq20171120
	 +--------------------------------------------------------------------------------------------------------
	 * @access static
	 +--------------------------------------------------------------------------------------------------------
	 * @param $uid	用户id
	 +--------------------------------------------------------------------------------------------------------
	 * @return float
	 +--------------------------------------------------------------------------------------------------------
	 **/
	public static function GetPermissionVersion($uid=false){
		return UserHelper::GetPermissionVersion($uid);
	}
	
	/**
	 +--------------------------------------------------------------------------------------------------------
	 * 获取用户以获得权限的平台
	 +--------------------------------------------------------------------------------------------------------
	 * @access static
	 +--------------------------------------------------------------------------------------------------------
	 * @param $uid	用户id
	 +--------------------------------------------------------------------------------------------------------
	 * @return array e.g. array("amazon","ebay");平台值为PlatformAccountApi::$platformList 里面的平台
	 +--------------------------------------------------------------------------------------------------------
	 **/
	public static function getUserAuthorizePlatform($uid=false){
		return UserHelper::getUserAuthorizePlatform($uid);
	}
	
	/**
	 +--------------------------------------------------------------------------------------------------------
	 * 获取当前页面需要的权限
	 +--------------------------------------------------------------------------------------------------------
	 * @access static
	 +--------------------------------------------------------------------------------------------------------
	 * @return array e.g. array("amazon","ebay");平台值为PlatformAccountApi::$platformList 里面的平台
	 +--------------------------------------------------------------------------------------------------------
	 **/
	public static function getCurrentPageRequiredAuth(){
		$authConfig = array_merge(
			require(__DIR__ . '/../config/params.php')
		);
		
		$requiredAuth = array();
		$module=\Yii::$app->controller->module->id;
		$controller=\Yii::$app->controller->id;
		$action=\Yii::$app->controller->action->id;
		if ($module=="app-eagle" and $controller=="site"){
			return $requiredAuth;
		}
		
		$requiredAuth = array();
		foreach($authConfig as $key=>$matchUrls){
			$isMatch = false;
			foreach($matchUrls as $matchUrl){
				$urlArr=explode("/", $matchUrl);
				if ((count($urlArr)==4 and $matchUrl=="/$module/$controller/$action")
				or (count($urlArr)==3 and $matchUrl=="/$module/$controller")
				or (count($urlArr)==2 and $matchUrl=="/$module")	){
					//命中
					$requiredAuth[] = $key;
				}
			}
		}
		
		return $requiredAuth;
	}
	
	/**
	 +--------------------------------------------------------------------------------------------------------
	 * 判断当前账号是否主账号
	 +--------------------------------------------------------------------------------------------------------
	 * @access static
	 +--------------------------------------------------------------------------------------------------------
	 * @return boolean
	 +--------------------------------------------------------------------------------------------------------
	 **/
	public static function isMainAccount(){
		return UserHelper::isMainAccount();
	}
	
	/**
	 +--------------------------------------------------------------------------------------------------------
	 * 获取正在启用的子账号数
	 +--------------------------------------------------------------------------------------------------------
	 * @access static
	 +--------------------------------------------------------------------------------------------------------
	 * @return
	 +--------------------------------------------------------------------------------------------------------
	 **/
	public static function getSubAccountNum(){
	    return UserHelper::getSubAccountNum();
	}
}

?>